CBSE Class 12 Computer Science – Detailed SQL Code Examples

2025–2026 Board Exam Preparation • Most Repeated & Important Queries

Most Important SQL Topics (2021–2025 PYQs Trend)

Detailed SQL Code Examples – Most Repeated Questions

1. Table Creation & Constraints

-- Create Employee Table with Constraints CREATE TABLE Employee ( EmpID INT PRIMARY KEY AUTO_INCREMENT, EmpName VARCHAR(50) NOT NULL, Salary DECIMAL(10,2), DeptID INT, HireDate DATE DEFAULT CURRENT_DATE, Email VARCHAR(100) UNIQUE, FOREIGN KEY (DeptID) REFERENCES Department(DeptID) ON DELETE SET NULL ON UPDATE CASCADE );

2. Insert, Update, Delete

-- Insert Multiple Records INSERT INTO Employee (EmpName, Salary, DeptID, Email) VALUES ('Rahul Sharma', 65000.00, 101, 'rahul@company.com'), ('Priya Singh', 72000.00, 102, 'priya@company.com'); -- Update Salary for Employees in Dept 101 UPDATE Employee SET Salary = Salary + 5000 WHERE DeptID = 101; -- Delete Employee with EmpID 5 DELETE FROM Employee WHERE EmpID = 5;

3. SELECT Queries – Most Important

-- Basic SELECT with WHERE & ORDER BY SELECT EmpName, Salary FROM Employee WHERE Salary > 50000 ORDER BY Salary DESC; -- Aggregate Functions with GROUP BY & HAVING SELECT DeptID, COUNT(*) AS TotalEmployees, AVG(Salary) AS AvgSalary FROM Employee GROUP BY DeptID HAVING COUNT(*) > 2; -- JOIN Example (Inner Join) SELECT e.EmpName, d.DeptName, e.Salary FROM Employee e INNER JOIN Department d ON e.DeptID = d.DeptID WHERE d.DeptName = 'IT'; -- Subquery Example – Employees with Salary > Average Salary SELECT EmpName, Salary FROM Employee WHERE Salary > (SELECT AVG(Salary) FROM Employee);

4. Advanced Queries – Very High Repeat

-- Second Highest Salary SELECT MAX(Salary) AS SecondHighest FROM Employee WHERE Salary < (SELECT MAX(Salary) FROM Employee); -- Employees earning more than their Department Average SELECT e.EmpName, e.Salary, d.DeptName FROM Employee e INNER JOIN ( SELECT DeptID, AVG(Salary) AS AvgDeptSalary FROM Employee GROUP BY DeptID ) AS dept_avg ON e.DeptID = dept_avg.DeptID WHERE e.Salary > dept_avg.AvgDeptSalary;

Exam Strategy – SQL (2026 Boards)

  1. Most repeated questions: Table creation with constraints, SELECT with JOIN/GROUP BY/HAVING, second highest salary, subqueries
  2. Mark distribution: 10–15 marks from SQL alone (2–3 questions)
  3. Tip: Always write exact syntax, use proper capitalization (SELECT, FROM, WHERE), mention table/column names clearly
  4. Time: 15–18 minutes for 10–12 mark SQL question
  5. Practice: Solve last 5 years papers + sample papers 2025–26, focus on output-based & error-finding questions

This guide covers the most important SQL topics & code examples for Class 12 Computer Science.